﻿#pragma once

#include <QtCore/QRegExp>
#include <QtGui/QSyntaxHighlighter>
#include <Global.h>

namespace Domy {

class XMLSyntaxHighlighter : public QSyntaxHighlighter
{
public:
    explicit XMLSyntaxHighlighter(QObject *parent = Q_NULLPTR);
    ~XMLSyntaxHighlighter();

    /**
     * @brief setSchemeItem
     * @note  设置配色
     * @param item         配色方案项
     * @param color        颜色
     * @param is_bold      粗体
     * @param is_italic    斜体
     * @param is_underline 下划线
     */
    void setSchemeItem(const ColorSchemeItem &item, const QColor &color,
                       bool is_bold, bool is_italic, bool is_underline);

    /**
     * @brief updateRules
     * @note  更新规则
     */
    void updateRules();

    /**
     * @brief setSearchHighlight
     * @note  设置搜索高亮
     * @param text              查找文本
     * @param bg_color          背景色
     * @param is_case_sensitive 大小写敏感
     * @param is_whole_word     整词
     */
    void setSearchHighlight(const QString &text, const QColor &bg_color,
                            bool is_case_sensitive, bool is_whole_word);

    /**
     * @brief clearSearchHighlight
     * @note  清除搜索高亮
     */
    void clearSearchHighlight();

protected:
    virtual void highlightBlock(const QString &text);

private:
    struct HighlightingRule
    {
        QRegExp         pattern;
        QTextCharFormat format;
    };
    QVector<HighlightingRule> m_highlight_rules;

    QRegExp m_comment_start_expression;
    QRegExp m_comment_end_expression;
    QRegExp m_search_exp;

    QTextCharFormat m_search_format;
    QTextCharFormat m_tag_format;
    QTextCharFormat m_attr_format;
    QTextCharFormat m_attr_content_format;
    QTextCharFormat m_comment_format;
};

} // namespace Domy
